package com.khai.app2;

import com.khai.sdk.api.SendService;
import com.khai.sdk.dto.*;
import feign.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * Created by KhaiLoon on 16/6/3.
 */
@SpringCloudApplication
@EnableFeignClients(basePackages = "com.khai.sdk.api")
@RestController
public class ApplicationApp2 {
    public static void main(String[] args) {
        new SpringApplicationBuilder(ApplicationApp2.class).web(true).run(args);
    }



    @Autowired
    private RestTemplate restTemplate;

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }

    @RequestMapping(value="/restTemplate")
    public String sendApp1() {
        // 根据应用名称调用服务
        String json = restTemplate.getForObject("http://app1/person", String.class);
        System.out.println("调用服务端");
        return json;
    }


    @Autowired
    private SendService sendService;


    @Bean
    public Logger.Level feignLoggerLevel() {
        return feign.Logger.Level.FULL;
    }


    @RequestMapping(value="/sendFegin")
    public Object sendFegin() {
        System.out.println("调用服务端");
        UserReqVo userReqVo = new UserReqVo();
        userReqVo.setUserName("oneName");

        ReqVo<UserReqVo> reqVo = new ReqVo<>();
        reqVo.setReqBody(userReqVo);
        ResVo<UserResVo> user = sendService.findUser(reqVo);
        return user;
    }

    //负载均衡
    @RequestMapping(value="/sendFegin2")
    public Object sendFegin2() {
        return restTemplate.getForObject("http://app1/person", Object.class);
    }



}
